var prefix = jsctx + "/invoiceProxyStation";

var invoiceRecord = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageInvoiceRecord/operator",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            beginTime: $("#beginTime").val().trim(),
                            nickname: $("#nickname").val().trim(),
                            addressee: $("#addressee").val().trim(),
                            status: $("#status").val().trim(),
                            endTime: $("#endTime").val().trim(),
                            addresseeTel: $("#addresseeTel").val().trim(),
                        };
                    },
                    columns: [
                        {
                            checkbox: true, field: '', width: '50', align: 'left',
                        },
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {
                            field: 'invoiceType', title: '发票类型',
                            width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '普通发票'
                                } else if (value == '1') {
                                    return '增值税专用发票'
                                } else if (value == '2') {
                                    return '增值税普通发票-电子发票'
                                } else if (value == '3') {
                                    return '增值税普通发票-纸质发票'
                                }
                            }
                        },
                        {field: 'nickname', title: '昵称', width: '60', align: 'left'},
                        {field: 'mobile', title: '手机号', width: '60', align: 'left'},
                        {field: 'amount', title: '开票金额', width: '60', align: 'left'},
                        {field: 'invoiceTitle', title: '发票抬头', width: '100', align: 'left'},
                        {field: 'addressee', title: '收件人', width: '60', align: 'left'},
                        {field: 'addresseeTel', title: '收件人手机', width: '100', align: 'left'},
                        {field: 'companyCredit', title: '公司统一组织编码', width: '100', align: 'left'},
                        {field: 'address', title: '发票地址', width: '100', align: 'left'},
                        {field: 'companyEmail', title: '发票邮箱', width: '100', align: 'left'},
                        {
                            field: 'sendType', title: '邮寄状态',
                            width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '否'
                                } else if (value == '1') {
                                    return '是'
                                }
                            }
                        },
                        {field: 'createdTime', title: '添加时间', width: '100', align: 'left'},
                        {field: 'lastUpdatedTime', title: '处理时间', width: '100', align: 'left'},
                        {
                            field: 'status', title: '状态',
                            width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '未审核'
                                } else if (value == '1') {
                                    return '审核通过'
                                } else if (value == '2') {
                                    return '审核不通过'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {

                                var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="编辑" onclick="invoiceRecord.editInvoiceApply(\''
                                    + row.invoiceApplyId
                                    + '\')">编辑</a> ';

                                var b = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="导出" onclick="invoiceRecord.exportInvoiceOrder(\''
                                    + row.invoiceApplyId
                                    + '\')">导出</a> ';

                                var c = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="通过" onclick="invoiceRecord.pass(\''
                                    + row.invoiceApplyId
                                    + '\')">通过</a> ';

                                var d = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="驳回" onclick="invoiceRecord.overrule(\''
                                    + row.invoiceApplyId
                                    + '\')">驳回</a> ';

                                var e = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="已通过" onclick="">已通过</a> ';

                                var f = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="已驳回" onclick="">已驳回</a> ';

                                if (row.status == '0') {
                                    return b + c + d;
                                } else if (row.status == '1') {
                                    return b + e;
                                } else if (row.status == '2') {
                                    return f;
                                }

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }


    //批量导出申请记录
    function selectInvoiceApply() {
        var rows = $("#exampleTable").bootstrapTable('getSelections');// 获得要删除的数据
        if (rows.length == 0) {// rows 主要是为了判断是否选中，下面的else内容才是主要
            alert("请先选择记录!");
            return;
        } else {
            var invoiceApplyList = new Array();// 声明一个数组
            $(rows).each(function () {// 通过获得别选中的来进行遍历
                if (this.status != 2) {
                    invoiceApplyList.push(this.invoiceApplyId); // cid为获得到的整条数据中的一列
                }
            });

            exportInvoiceApply(invoiceApplyList)
        }
    }


    function exportInvoiceApply(invoiceApplyList) {
        location.href = prefix + "/exportInvoiceApply?invoiceApplyList=" + invoiceApplyList;
    }

    function pass(invoiceApplyId) {
        layer.confirm('确定要通过？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: jsctx + "/invoiceApply/review",
                type: "post",
                data: {
                    'invoiceApplyId': invoiceApplyId,
                    'status': '0'

                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        countInvoiceByStation()
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function overrule(invoiceApplyId) {
        layer.confirm('确定要驳回？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: jsctx + "/invoiceApply/review",
                type: "post",
                data: {
                    'invoiceApplyId': invoiceApplyId,
                    'status': '1'
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        countInvoiceByStation()
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function exportInvoiceOrder(invoiceApplyId) {
        location.href = jsctx + "/invoiceApply/exportInvoiceOrder?invoiceApplyId=" + invoiceApplyId;
    }

    function goBack() {
        window.history.back();
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        pass: pass,
        overrule: overrule,
        selectInvoiceApply: selectInvoiceApply,
        exportInvoiceOrder: exportInvoiceOrder
    }

})();
